//
//  WBHomeController.swift
//  weibo
//
//  Created by 李江波 on 16/10/6.
//  Copyright © 2016年 lijiangbo. All rights reserved.
//

import UIKit

private let cellID = "cellID"


class WBHomeController: WBBaseController {

    fileprivate lazy var data = [String]()
    
    
    /// 加载数据
    override func loadData()  {
        
        //延迟模拟刷新时间
    DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 1) {
        
        if self.isPullup {
            for i in 0..<20 {
                self.data.append("上拉" + i.description)
            }
        }else{
            for i in 0..<20 {
                self.data.insert(i.description, at: i)
            }
        }

        //刷新tableview
        self.tableView?.reloadData()
        
        //去掉菊花
        self.refreshControl?.endRefreshing()
        
        self.isPullup = false
       }
        
    }
    
    @objc fileprivate func pushNext(){
    
        let vc = WBTestViewController()
        
        navigationController?.pushViewController(vc, animated: true)
    }

}
extension WBHomeController {

    override func setupTableView() {
        super.setupTableView()
        
        let btn: UIButton = UIButton(title: "好友", normalColor: UIColor.gray, highlightedColor: UIColor.orange)
        
        btn.addTarget(self, action: #selector(pushNext), for: .touchUpInside)
        
        navItem.leftBarButtonItem = UIBarButtonItem(customView: btn)
        
        tableView?.register(UITableViewCell.self, forCellReuseIdentifier: cellID)
    }
    
   
}


// MARK: - 实线数据源方法
extension WBHomeController{

    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return data.count
    }
    
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        let cell = tableView.dequeueReusableCell(withIdentifier: cellID, for: indexPath)
        
        cell.textLabel?.text = data[indexPath.row]
        
        return cell
    }
}
